package br.com.dao;


import br.com.dao.util.GenericDao;
import br.com.model.Locacao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LocacaoDao implements GenericDao<Locacao> {

    private Connection connection;

    public LocacaoDao(Connection connection) {
        this.connection = connection;
    }

     @Override
    public void save(Locacao locacao) {
        Integer id = null;
        PreparedStatement stmt = null;
        try {
            this.connection.setAutoCommit(false);
            String sql = "insert into locacao"
                    + " (data)"
                    + " values (?)";
            
            stmt = this.connection.prepareStatement(sql);
            stmt.setDate(1, locacao.getData());
            stmt.executeUpdate();
            this.connection.commit();
            
            sql = "select MAX(id) as id from locacao"; 
            stmt = this.connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            rs.next();
            id = Integer.parseInt(rs.getString(1));
            
            sql = "insert into locacao_itemsLocados(id_locacao, itemsLocados)"
                    + " values (?,?)";
            stmt = this.connection.prepareStatement(sql);
           
            for (int i = 0; i < locacao.getItemsLocados().toArray().length; i++) {
                stmt.setInt(1, id);
                stmt.setString(2, locacao.getItemsLocados().toArray()[i].toString());
                stmt.executeUpdate();
                this.connection.commit();
            }

            
            sql = "update item set estado ='LOCADO' where numeroserie= ?";
            stmt = this.connection.prepareStatement(sql);
            for (int i = 0; i < locacao.getItemsLocados().toArray().length; i++) {
                stmt.setString(1, locacao.getItemsLocados().get(i).getItem().getNumeroSerie().getCodigoBarras());
                stmt.executeUpdate();
                this.connection.commit();
                
            }

        } catch (SQLException e) {
            e.printStackTrace();
            if(this.connection != null){
                try{
                    System.err.print("Transaction is beign rolled back");
                    this.connection.rollback();
                }catch(SQLException excep){
                    System.err.println(excep);
                }
            }
        }
        finally{
               if(stmt != null){
                   try{
                       stmt.close();
                   }catch(SQLException ex){
                       Logger.getLogger(LocacaoDao.class.getName()).log(Level.SEVERE, null, ex);
                   }
               }
                try {
                    this.connection.setAutoCommit(true);
                } catch (SQLException ex) {
                    Logger.getLogger(LocacaoDao.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

    }

    @Override
    public void delete(Locacao locacao) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Locacao> list() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Locacao findById(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public LinkedList<Locacao> listar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Locacao getObjectById(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public LinkedList<Locacao> getObjectByName(String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
